package com.bianguo.android.functiondemo.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.util.Scanner;

public class NioClient {

    private static Charset charset = Charset.forName("UTF-8");

    public static void main(String[] args) {

        try {
            SocketChannel sc = SocketChannel.open();
            boolean connect = sc.connect(new InetSocketAddress("localhost", 9200));
            System.out.println("connected : " + connect);
            Scanner scanner = new Scanner(System.in);
            System.out.print("请输入: ");
            String message = scanner.nextLine();
            ByteBuffer bf = ByteBuffer.wrap(message.getBytes(charset));

            while (bf.hasRemaining()) {
                int writeCount = sc.write(bf);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
